//
//  ViewController.m
//  OCRProject
//
//  Created by 郭翰林 on 2017/10/24.
//  Copyright © 2017年 郭翰林. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,retain) UIImage *parseImg;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mButton.layer.backgroundColor=[UIColor orangeColor].CGColor;
    self.mButton.layer.cornerRadius=5;
    [self.mButton addTarget:self action:@selector(startParseImg) forControlEvents:UIControlEventTouchUpInside];
    //设置等待框样式
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
//    self.parseImg=[UIImage imageNamed:@"eng"];//英文图片
    self.parseImg=[UIImage imageNamed:@"image_sample"];//中文图片
    self.mImageView.image=self.parseImg;
    // Do any additional setup after loading the view, typically from a nib.
}


-(void)startParseImg{
    [SVProgressHUD showWithStatus:@"正在识别分析图片中...."];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //OCR图片识别
//        G8RecognitionOperation  *operation=[[G8RecognitionOperation alloc] initWithLanguage:@"chi_sim"];//中文
//        G8RecognitionOperation  *operation=[[G8RecognitionOperation alloc] initWithLanguage:@"eng"];//英文
        G8RecognitionOperation  *operation=[[G8RecognitionOperation alloc] initWithLanguage:@"chi_sim+eng"];//中文与英文
        // Configure inner G8Tesseract object as described before
        operation.tesseract.image = [self.parseImg g8_blackAndWhite];
        
        // Setup the recognitionCompleteBlock to receive the Tesseract object
        // after text recognition. It will hold the recognized text.
        operation.recognitionCompleteBlock = ^(G8Tesseract *recognizedTesseract) {
            // Retrieve the recognized text upon completion
            [SVProgressHUD dismiss];
            NSLog(@"\n%@", [recognizedTesseract recognizedText]);
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"分析结果" message:[NSString stringWithFormat:@"\n%@",[recognizedTesseract recognizedText]] delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
            [alert show];
        };
        // Add operation to queue
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue addOperation:operation];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
